package com.example.threadtest.controller;

import com.example.threadtest.entity.User;
import com.example.threadtest.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2021-03-24 18:04:48
 */
@Controller
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 跳转到注册页面
     * @return
     */
    @RequestMapping(value = "register")
    public String register(){
        return "register";
    }
    /**
     * 跳转到登录页面
     * @return
     */
    @RequestMapping(value = "login")
    public String login(){
        return "login";
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    @ResponseBody
    public User selectOne(Integer id) {
        return this.userService.queryById(id);
    }

    /**
     * 添加用户
     *
     * @param user 插入的用户信息
     * @return 单条数据
     */
    @PostMapping(value = "addUser")
    @ResponseBody
    public User addUser(@Valid User user, BindingResult bindingResult) {
        System.out.println("user="+user.toString());
        System.out.println("bindingResult=");
        System.out.println("------------------1------------------");
        System.out.println(bindingResult);
        System.out.println("------------------2------------------");
        User user1 = userService.insert(user);
        return user1;
    }
    /**
     * 添加用户
     *
     * @param user 插入的用户信息
     * @return 单条数据
     */
    @PostMapping(value = "userLogin")
    @ResponseBody
    public User userLogin(@Validated User user) {
        System.out.println("user="+user.toString());
        User user1 = userService.insert(user);
        return user1;
    }

}